package com.ys.service.account.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.dto.EditDto;
import com.ys.entity.account.TbAdvanceType;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.account.TbAdvanceTypeMapper;
import com.ys.service.account.TbAdvanceTypeService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;


/**
 * 预支类型维护
 */
@Service("tbAdvanceTypeService")
public class TbAdvanceTypeServiceImpl implements TbAdvanceTypeService {
	private static final Logger logger = LoggerFactory.getLogger(TbAdvanceTypeServiceImpl.class);

	@Autowired
	private TbAdvanceTypeMapper tbAdvanceTypeMapper;

	@Autowired
	private CheckUtil checkUtil;

	/**
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		TbAdvanceType tbAdvanceType = JSON.parseObject(JSON.toJSONString(params), TbAdvanceType.class);
		//生成编码
		String codeTop = "YZZHLX";
		TbAdvanceType tbAdvanceTypeMax = new TbAdvanceType().setOrderby("A.Id DESC").queryFirst();
		if(tbAdvanceTypeMax!=null && tbAdvanceTypeMax.getCode()!=null ){
			long id = tbAdvanceTypeMax.getId();
			long codeNum = id ;//编码从0开始，比id少1
			String codeStr = codeNum+"";
			if(codeNum<100){
				codeStr = String.format("%0"+3+"d", codeNum);//小于3位在前面补0
			}
			tbAdvanceType.setCode(codeTop+codeStr);
		}else {
			tbAdvanceType.setCode("YZZHLX000");
		}
		tbAdvanceTypeMapper.insert(tbAdvanceType);
		return R.ok(EditDto.closeAndReload());
		//return R.ok(AddDto.gotoEdit("/web/finance/tbAdvanceType/toEdit?id=" + tbAdvanceType.getId()));
	}


	/**
	 * 修改
	 */
	@Override
	public R update(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		TbAdvanceType tbAdvanceType = JSON.parseObject(JSON.toJSONString(params), TbAdvanceType.class);
		if(tbAdvanceType.getCode().equals("YZZHLX000") || tbAdvanceType.getCode().equals("YZZHLX001")){
			if(tbAdvanceType.getStatus()==0){
				return R.error(tbAdvanceType.getName()+"不能停用！");
			}
		}
		tbAdvanceTypeMapper.update(tbAdvanceType);
		//return R.ok();
		return R.ok(EditDto.closeAndReload());
	}


	/**
	 * 停用
	 */
	@Override
	public R updateStatusStop(Long id) throws Exception {
		TbAdvanceType tbAdvanceType = new TbAdvanceType();
		tbAdvanceType.setId(id);
		tbAdvanceType.setStatus(0);
		tbAdvanceTypeMapper.update(tbAdvanceType);
		return R.ok();
	}



	/**
	 * 参数验证
	 */
	public String checkParams(Map<String, Object> params, Long menuId) throws Exception {
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+menuId)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		String backMsg = checkUtil.checkTableColLengthAndDataType("TB_UserTaxManage",listField,params,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
		return backMsg;
	}





}
